﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.HumanResources;
using BusinessObjects.HumanResources;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer.HumanResources
{
    public class SqlServerTimeSheetSignDao : ITimeSheetSignDao
    {
        public List<TimeSheetSign> GetTimeSheetSigns()
        {
            string procedures = "Get_All_CountryRegions";

            return Db.ReadList(procedures, true, Make, null);
        }

        public List<TimeSheetSign> GetTimeSheetSignsActive(bool isActive)
        {
            string procedures = "Get_All_TimeSheetSigns_ByCondition";
            int _isActive = 0;

            if (isActive == true) { _isActive = 1; }
            else { _isActive = 0; }

            object[] parms = { "@WhereClause", " WHERE IsActive = " + _isActive };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<TimeSheetSign> GetTimeSheetSignPaged(int currentPage)
        {
            string procedures = "Get_Paged_TimeSheetSigns";

            object[] parms = {  "@WhereClause", null, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public int GetTotalPage()
        {
            string procedures = "Get_PageSize_TimeSheetSign";

            object[] parms = { "@WhereClause", null };
            return Db.ReadTotalPage(procedures, true, parms);
        }

        public TimeSheetSign GetTimeSheetSign(string timeSheetSignId)
        {
            string procedures = "Get_All_TimeSheetSigns_ByCondition";

            object[] parms = { "@WhereClause", " WHERE TimeSheetSignID = '" + timeSheetSignId + "'" };
            return Db.Read(procedures, true, Make, parms);
        }

        public void InsertTimeSheetSign(TimeSheetSign timeSheetSign)
        {
            string sql = "Insert_TimeSheetSign";

            timeSheetSign.TimeSheetSignID = Db.Insert(sql, true, Take(timeSheetSign));
        }

        public void UpdateTimeSheetSign(TimeSheetSign timeSheetSign)
        {
            string sql = "Update_TimeSheetSign";

            Db.Update(sql, true, Take(timeSheetSign));
        }

        public void DeleteTimeSheetSign(TimeSheetSign timeSheetSign)
        {
            string sql = "Delete_TimeSheetSign";

            Db.Update(sql, true, Take(timeSheetSign.TimeSheetSignID));
        }

        private object[] Take(TimeSheetSign timeSheetSign)
        {
            return new object[]  
            {
                "@TimeSheetSignID", timeSheetSign.TimeSheetSignID,
                "@TimeSheetSignCode", timeSheetSign.TimeSheetSignCode,
                "@TimeSheetSignName", timeSheetSign.TimeSheetSignName,
                "@SalaryRate", timeSheetSign.SalaryRate,
                "@Description", timeSheetSign.Description,
                "@IsActive", timeSheetSign.IsActive
            };
        }

        private object[] Take(string timeSheetSignId)
        {
            return new object[]  
            {
                "@TimeSheetSignID", timeSheetSignId,
            };
        }

        private static Func<IDataReader, TimeSheetSign> Make = reader =>
            new TimeSheetSign
            {
                TimeSheetSignID = reader["TimeSheetSignID"].AsString(),
                TimeSheetSignCode = reader["TimeSheetSignCode"].AsString(),
                TimeSheetSignName = reader["TimeSheetSignName"].AsString(),
                SalaryRate = reader["SalaryRate"].AsDecimal(),
                Description = reader["Description"].AsString(),
                IsActive = reader["IsActive"].AsBool()
            };
    }
}
